package com.mk.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.mk.base.exception.ApplicationException;
import com.mk.base.exception.StatusCode;
import com.mk.base.service.impl.BaseService;
import com.mk.common.BeanCopier;
import com.mk.common.Config;
import com.mk.mapper.TagMapper;
import com.mk.model.entity.Tag;
import com.mk.model.request.TagRequest;
import com.mk.service.ITagService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * @Author: yq
 * @Description:
 * @Date: Created in 3:19 PM 3/15/18
 */
@Service
public class TagService extends BaseService<TagMapper,Tag> implements ITagService {

    @Override
    @Transactional
    public Tag saveTag(TagRequest request){
        if(request == null)
            throw new ApplicationException(StatusCode.BAD_REQUEST.getCode(),"请传入资料包信息");
        Tag exist = getByNum(request.getNum());
        if (exist != null)
            throw new ApplicationException(StatusCode.UNIQUE_KE.getCode(),"添加的资料包的编号已存在");
        Tag tag = BeanCopier.copy(request,Tag.class);
        Date nowDate = new Date();
        tag.setCreateTime(nowDate);
        tag.setUpdateTime(nowDate);
        if(super.insert(tag))
            return tag;
        else
            throw new ApplicationException(StatusCode.CONFLICT.getCode(),"添加资料包信息失败");
    }

    @Override
    public Boolean updateTag(com.mk.model.request.TagRequest request) {
        if(request == null || request.getId() == null)
            throw new ApplicationException(StatusCode.BAD_REQUEST.getCode(),"请选择编辑的资料包信息");
        Tag exist = get(request.getId());
        if(exist == null)
            throw new ApplicationException(StatusCode.CONFLICT.getCode(),"编辑的资料包信息不存在");
        if(!StringUtils.isEmpty(request.getNum())){
            exist = getByNum(request.getNum());
            if(exist != null && !exist.getId().equals(request.getId()))
                throw new ApplicationException(StatusCode.UNIQUE_KE.getCode(),"资料包的编号不可重复");
        }
        exist = BeanCopier.copy(request,Tag.class);
        exist.setUpdateTime(new Date());
        return super.updateById(exist);
    }

    @Override
    public Boolean deleteTag(Long id) {
        if(id == null)
            throw new ApplicationException(StatusCode.BAD_REQUEST.getCode(),"请选择需要删除的资料包");
        Tag exist = get(id);
        if(exist == null)
            throw new ApplicationException(StatusCode.CONFLICT.getCode(),"删除的资料包不存在");
        exist.setUpdateTime(new Date());
        exist.setIsDeleted(Config.ABLE_CONFIG.UNABLE);
        return super.updateById(exist);
    }

    @Override
    public Boolean deleteByPacId(Long pacId) {
        if(pacId == null)
            throw new ApplicationException(StatusCode.BAD_REQUEST.getCode(),"请选择需要删除的资料包");
        Tag exist = getByPacId(pacId);
        if(exist == null)
            throw new ApplicationException(StatusCode.CONFLICT.getCode(),"删除的资料包信息不存在");
        exist.setIsDeleted(Config.ABLE_CONFIG.UNABLE);
        exist.setUpdateTime(new Date());
        return super.updateById(exist);
    }

    @Override
    public Boolean saveOnlyTag(Tag tag) {
        Date nowDate = new Date();
        tag.setCreateTime(nowDate);
        tag.setUpdateTime(nowDate);
        return super.updateById(tag);
    }

    @Override
    public Tag get(Long id) {
        return super.selectById(id);
    }

    @Override
    public Tag getByPacId(Long pacId) {
        return super.selectOne(new EntityWrapper<Tag>()
                .eq("pac_id",pacId).eq(Config.ABLE_CONFIG.ABLE_COLUMN,Config.ABLE_CONFIG.ABLE));
    }

    @Override
    public Tag getByNum(String num) {
        return super.selectOne(new EntityWrapper<Tag>()
                .eq("num",num).eq(Config.ABLE_CONFIG.ABLE_COLUMN,Config.ABLE_CONFIG.ABLE));
    }
}
